<?php
/**
 * 
 * Enter description here ...
 * @author Rosanna Paberli Jimenez Rosario.
 *
 */
class DocumentosEstatus extends Controller
{
	
	public function __construct()
	{
		
		parent::Controller();
		$this->load->model(array("documentosestatusmodel"));
	}
	

	public function index()
	{
		$this->load->view('main',array('content_page' => 'documentosestatus_list.php', 'menutop' => 0));	
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $filtro
	 */
	public function data()
	{
		
		 $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		 $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'id_documento'; // Columna para el order by
		 $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		 $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;

		 $start = $limit * $page - $limit;
		     if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );

	    $records = $this->documentosestatusmodel->loadRegistros($start, $limit, $sortname, $sortorder);
	    
		$totalRecords = count($records);
		
		 $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) 
		       $page = $totalPages;

		    $json_output = array(
		            'page'		=> $page,
		            'total'		=> $totalPages,
		            'records'	=> $totalRecords,
		            'rows'		=> array()
		        );

		    if($records != false){
			     foreach($records as $objetos){
			     	
				     $arr = $json_output['rows'][] = array(
				          'id_documento'              => $objetos->getId_documento(),
				          'titulo_documento'          => $objetos->getTitulo_documento() 
				      );
			      }
		      }

		 echo json_encode($json_output);
	}
	
	
	public function add()
	{
		$this->load->view('main', array('content_page' => 'documentosestatus_add',
										'menutop'      => 0));
	}
	
	public function edit($idDocumento)
	{
		
		$registros = $this->documentosestatusmodel->loadRegistrosById($idDocumento);
		
		$this->load->view('main', array('content_page' => 'documentoestatus_edit',
								        'menutop'      => 0,
										'registros'    => $registros));
	}
	
	public function delete()
	{
		$id = $this->input->post('delete_rows');
   		$this->documentosestatusmodel->eliminar($id);
	}
	
	public function save()
	{
		
	   $data = array("titulo_documento" => $this->input->post('titulo_documento'));  
					
   	
   	   if ($this->input->post('id_documento') == false) {
   	   	   if ($this->documentosestatusmodel->agregar($data) == true) {
   	   	   	   $mensaje = "El resgistro se agrego exitosamente.";
   	   	   	   $router = true;
   	   	   }else{
   	   	       $mensaje = "Hubo un error...";
   	   	       $router = false;
   	   	   }
   	   	  
   	   }else{
   	       if ($this->documentosestatusmodel->editar($data, $this->input->post('id_documento')) == true) {
   	   	   	   $mensaje = "El resgistro se edito exitosamente";
   	   	   	   $router = true;
   	   	   }else{
   	   	       $mensaje = "Hubo un error...";
   	   	       $router = false;
   	   	   }
   	   }
   	   
   	   $this->session->set_flashdata('error_title',  $mensaje);
   	   $this->session->set_flashdata('error_router', $router);
   	   redirect('documentosestatus');
	}
	
	
}